package com.andreamerla.petslife.model;

import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import com.andreamerla.petslife.PetsLifeApplication;
import com.andreamerla.petslife.util.Utils;
import com.google.android.gms.maps.model.LatLng;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: a.merla
 * Date: 17/07/13
 * Time: 17.29
 */
public class Placemark {
    private static Geocoder geocoder;

    private String name;
    private String description;
    private LatLng latLng;
    private Address address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LatLng getLatLng() {
        return latLng;
    }

    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }

    public Address getAddress() {
        if (address == null)
            loadAddress();
        return address;
    }

    public void loadAddress() {
        if (latLng != null && Geocoder.isPresent()) {
            try {
                List<Address> addressList = getGeocoder().getFromLocation(latLng.latitude, latLng.longitude, 1);
                if (addressList.size() > 0)
                    address = addressList.get(0);
            } catch (IOException e) {
                Log.e(Utils.getLogTag(this.getClass()), "Error retrieving address for " + name + " (" + latLng.latitude + ", " + latLng.longitude + ")");
                e.printStackTrace();
            }
        }
    }

    private static Geocoder getGeocoder() {
        if (geocoder == null)
            geocoder = new Geocoder(PetsLifeApplication.getAppContext(), Locale.ITALY);
        return geocoder;
    }
}
